package cn.hallele.leinsteining.ms.infrastructure.auditlog.client;

import cn.hallele.leinsteining.ms.infrastructure.auditlog.properties.AuditLogProperties;
import cn.hutool.extra.spring.SpringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

/**
 * 审计日志客户端
 *
 * @author anle5
 * @since 2024/7/28 16:22
 */
public class AuditLogClient {
    private static final Logger log = LoggerFactory.getLogger(AuditLogClient.class);
    private final AuditLogProperties properties;

    private final StreamBridge streamBridge = SpringUtil.getBean(StreamBridge.class);

    public AuditLogClient(AuditLogProperties properties) {
        if (properties == null || StringUtils.isBlank(properties.getStreamName())) {
            log.error("Stream name is null or empty");
        }
        this.properties = properties;
        initConnect();
    }

    /**
     * 链接
     */
    private void initConnect() {

    }

    public boolean sendMessage(Object message) {
        Message<Object> build = MessageBuilder.withPayload(message).build();
        return this.streamBridge.send(this.properties.getStreamName(), build);
    }
}
